﻿// ===============================================================================
// Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// 作者:焦元义
// ===============================================================================

using System;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Helper;
using XCI.Windows;

namespace XCI.Sys.Forms
{
    public partial class FrmDicCategoryEdit : BaseForm
    {
        public FrmDicCategoryEdit(EditFormArgs args)
        {
            InitializeComponent();
            layoutControl.Args = args;
        }

        private async void FrmDicCategoryEdit_Load(object sender, EventArgs e)
        {
            await layoutControl.BindingModelAsync();
        }

        private async void btnSave_Click(object sender, EventArgs e)
        {
            await layoutControl.SaveModelAsync();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogOk();
        }

        private void layoutControl_BindingForm(object sender, EditFormPanelBindingEventArgs e)
        {
            e.Task = async () =>
            {
                var model = await SysDicService.Instance.GetOrCreateDicCategoryAsync(e.Args);
                bindingSource.DataSource = model;
                await parentTreeList.SetDataSource(e.Created, model);
                WinFormHelper.SetTreeRootValue(parentTreeList, e.ParentId);
            };
        }

        private void layoutControl_SaveForm(object sender, EditFormPanelSaveEventArgs e)
        {
            e.Task = () => SysDicService.Instance.SaveDicCategoryAsync(e.Args.Created, (SysDicCategory)e.Model);
        }

        private async void layoutControl_ResetForm(object sender, EditFormPanelResetEventArgs e)
        {
            bindingSource.DataSource = await SysDicService.Instance.NewDicCategoryAsync(layoutControl.Args.ParentId);
            await layoutControl.BindingModelAsync();
        }
    }
}